﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package org.aswing {
    import flash.display.*;
    import flash.geom.*;
    import flash.utils.*;
    import org.aswing.geom.*;
    import flash.text.*;

    public class AsWingUtils {

        public static var CENTER:Number = AsWingConstants.CENTER;
        public static var TOP:Number = AsWingConstants.TOP;
        public static var LEFT:Number = AsWingConstants.LEFT;
        public static var BOTTOM:Number = AsWingConstants.BOTTOM;
        public static var RIGHT:Number = AsWingConstants.RIGHT;
        public static var HORIZONTAL:Number = AsWingConstants.HORIZONTAL;
        public static var VERTICAL:Number = AsWingConstants.VERTICAL;
        private static var TEXT_FIELD:TextField = new TextField();
        private static var TEXT_FONT:ASFont = null;
        private static var weakComponentDic:Dictionary = new Dictionary(true);
        private static var TEXT_FIELD_EXT:TextField = new TextField();

        public static function createSprite(_arg1:DisplayObjectContainer=null, _arg2:String=null):Sprite{
            var _local3:Sprite = new Sprite();
            _local3.focusRect = false;
            if (_arg2 != null){
                _local3.name = _arg2;
            };
            _local3.mouseEnabled = false;
            if (_arg1 != null){
                _arg1.addChild(_local3);
            };
            return (_local3);
        }
        public static function createLabel(_arg1:DisplayObjectContainer=null, _arg2:String=null):TextField{
            var _local3:TextField = new TextField();
            _local3.focusRect = false;
            if (_arg2 != null){
                _local3.name = _arg2;
            };
            _local3.selectable = false;
            _local3.mouseEnabled = false;
            _local3.mouseWheelEnabled = false;
            _local3.autoSize = TextFieldAutoSize.LEFT;
            _local3.tabEnabled = false;
            if (_arg1 != null){
                _arg1.addChild(_local3);
            };
            return (_local3);
        }
        public static function createShape(_arg1:DisplayObjectContainer=null, _arg2:String=null):Shape{
            var _local3:Shape = new Shape();
            if (_arg2 != null){
                _local3.name = _arg2;
            };
            if (_arg1 != null){
                _arg1.addChild(_local3);
            };
            return (_local3);
        }
        public static function isDisplayObjectShowing(_arg1:DisplayObject):Boolean{
            if ((((_arg1 == null)) || ((_arg1.stage == null)))){
                return (false);
            };
            while (((!((_arg1 == null))) && ((_arg1.visible == true)))) {
                if (_arg1 == _arg1.stage){
                    return (true);
                };
                _arg1 = _arg1.parent;
            };
            return (false);
        }
        public static function isAncestor(_arg1:Component, _arg2:Component):Boolean{
            if ((((_arg1 == null)) || ((_arg2 == null)))){
                return (false);
            };
            var _local3:DisplayObjectContainer = _arg2.parent;
            while (_local3 != null) {
                if (_local3 == _arg1){
                    return (true);
                };
                _local3 = _local3.parent;
            };
            return (false);
        }
        public static function isAncestorComponent(_arg1:Component, _arg2:Component):Boolean{
            if ((((((_arg1 == null)) || ((_arg2 == null)))) || (!((_arg1 is Container))))){
                return (false);
            };
            var _local3:Container = _arg2.getParent();
            while (_local3 != null) {
                if (_local3 == _arg1){
                    return (true);
                };
                _local3 = _local3.getParent();
            };
            return (false);
        }
        public static function isAncestorDisplayObject(_arg1:DisplayObjectContainer, _arg2:DisplayObject):Boolean{
            if ((((_arg1 == null)) || ((_arg2 == null)))){
                return (false);
            };
            var _local3:DisplayObjectContainer = _arg2.parent;
            while (_local3 != null) {
                if (_local3 == _arg1){
                    return (true);
                };
                _local3 = _local3.parent;
            };
            return (false);
        }
        public static function getStageMousePosition(_arg1:Stage=null):IntPoint{
            if (_arg1 == null){
                _arg1 = AsWingManager.getStage();
            };
            return (new IntPoint(_arg1.mouseX, _arg1.mouseY));
        }
        public static function getScreenCenterPosition():IntPoint{
            var _local1:IntRectangle = getVisibleMaximizedBounds();
            return (new IntPoint((_local1.x + (_local1.width / 2)), (_local1.y + (_local1.height / 2))));
        }
        public static function centerLocate(_arg1:JPopup):void{
            var _local2:IntPoint = getScreenCenterPosition();
            _local2.x = Math.round((_local2.x - (_arg1.getWidth() / 2)));
            _local2.y = Math.round((_local2.y - (_arg1.getHeight() / 2)));
            _arg1.setLocation(_local2);
        }
        public static function getVisibleMaximizedBounds(_arg1:DisplayObject=null):IntRectangle{
            var _local6:Point;
            var _local2:Stage = ((_arg1 == null)) ? null : _arg1.stage;
            if (_local2 == null){
                _local2 = AsWingManager.getStage();
            };
            if (_local2 == null){
                return (new IntRectangle(200, 200));
            };
            if (_local2.scaleMode != StageScaleMode.NO_SCALE){
                return (new IntRectangle(0, 0, _local2.stageWidth, _local2.stageHeight));
            };
            var _local3:Number = _local2.stageWidth;
            var _local4:Number = _local2.stageHeight;
            var _local5:IntRectangle = new IntRectangle(0, 0, _local3, _local4);
            if (_arg1 != null){
                _local6 = _arg1.globalToLocal(new Point(0, 0));
                _local5.setLocation(new IntPoint(_local6.x, _local6.y));
            };
            return (_local5);
        }
        public static function applyTextFontAndColor(_arg1:TextField, _arg2:ASFont, _arg3:ASColor):void{
            applyTextFont(_arg1, _arg2);
            applyTextColor(_arg1, _arg3);
        }
        public static function applyTextFont(_arg1:TextField, _arg2:ASFont):void{
            _arg2.apply(_arg1);
        }
        public static function applyTextFormat(_arg1:TextField, _arg2:TextFormat):void{
            _arg1.setTextFormat(_arg2);
        }
        public static function applyTextColor(_arg1:TextField, _arg2:ASColor):void{
            if (_arg1.textColor !== _arg2.getRGB()){
                _arg1.textColor = _arg2.getRGB();
            };
            if (_arg1.alpha !== _arg2.getAlpha()){
                _arg1.alpha = _arg2.getAlpha();
            };
        }
        public static function layoutCompoundLabel(_arg1:Component, _arg2:ASFont, _arg3:String, _arg4:Icon, _arg5:int, _arg6:int, _arg7:int, _arg8:int, _arg9:IntRectangle, _arg10:IntRectangle, _arg11:IntRectangle, _arg12:int):String{
            var _local21:IntDimension;
            var _local22:Number;
            if (_arg4 != null){
                _arg10.width = _arg4.getIconWidth(_arg1);
                _arg10.height = _arg4.getIconHeight(_arg1);
            } else {
                _arg10.width = (_arg10.height = 0);
            };
            var _local13:Boolean = (((_arg3 == null)) || ((_arg3 == "")));
            if (_local13){
                _arg11.width = (_arg11.height = 0);
            } else {
                _local21 = inter_computeStringSize(_arg2, _arg3);
                _arg11.width = _local21.width;
                _arg11.height = _local21.height;
            };
            var _local14:Number = (((_local13) || ((_arg4 == null)))) ? 0 : _arg12;
            if (!_local13){
                if (_arg8 == CENTER){
                    _local22 = _arg9.width;
                } else {
                    _local22 = (_arg9.width - (_arg10.width + _local14));
                };
                if (_arg11.width > _local22){
                    _arg3 = layoutTextWidth(_arg3, _arg11, _local22, _arg2);
                };
            };
            if (_arg7 == TOP){
                if (_arg8 != CENTER){
                    _arg11.y = 0;
                } else {
                    _arg11.y = -((_arg11.height + _local14));
                };
            } else {
                if (_arg7 == CENTER){
                    _arg11.y = ((_arg10.height / 2) - (_arg11.height / 2));
                } else {
                    if (_arg8 != CENTER){
                        _arg11.y = (_arg10.height - _arg11.height);
                    } else {
                        _arg11.y = (_arg10.height + _local14);
                    };
                };
            };
            if (_arg8 == LEFT){
                _arg11.x = -((_arg11.width + _local14));
            } else {
                if (_arg8 == CENTER){
                    _arg11.x = ((_arg10.width / 2) - (_arg11.width / 2));
                } else {
                    _arg11.x = (_arg10.width + _local14);
                };
            };
            var _local15:Number = Math.min(_arg10.x, _arg11.x);
            var _local16:Number = (Math.max((_arg10.x + _arg10.width), (_arg11.x + _arg11.width)) - _local15);
            var _local17:Number = Math.min(_arg10.y, _arg11.y);
            var _local18:Number = (Math.max((_arg10.y + _arg10.height), (_arg11.y + _arg11.height)) - _local17);
            var _local19:Number = 0;
            var _local20:Number = 0;
            if (_arg5 == TOP){
                _local20 = (_arg9.y - _local17);
            } else {
                if (_arg5 == CENTER){
                    _local20 = ((_arg9.y + (_arg9.height / 2)) - (_local17 + (_local18 / 2)));
                } else {
                    _local20 = ((_arg9.y + _arg9.height) - (_local17 + _local18));
                };
            };
            if (_arg6 == LEFT){
                _local19 = (_arg9.x - _local15);
            } else {
                if (_arg6 == RIGHT){
                    _local19 = ((_arg9.x + _arg9.width) - (_local15 + _local16));
                } else {
                    _local19 = ((_arg9.x + (_arg9.width / 2)) - (_local15 + (_local16 / 2)));
                };
            };
            _arg11.x = (_arg11.x + _local19);
            _arg11.y = (_arg11.y + _local20);
            _arg10.x = (_arg10.x + _local19);
            _arg10.y = (_arg10.y + _local20);
            return (_arg3);
        }
        private static function inter_computeStringSize(_arg1:ASFont, _arg2:String):IntDimension{
            TEXT_FIELD.text = _arg2;
            if (TEXT_FONT != _arg1){
                _arg1.apply(TEXT_FIELD);
                TEXT_FONT = _arg1;
            };
            return (new IntDimension(Math.ceil(TEXT_FIELD.width), Math.ceil(TEXT_FIELD.height)));
        }
        private static function inter_computeStringWidth(_arg1:ASFont, _arg2:String):Number{
            TEXT_FIELD.text = _arg2;
            if (TEXT_FONT != _arg1){
                _arg1.apply(TEXT_FIELD);
                TEXT_FONT = _arg1;
            };
            return (TEXT_FIELD.width);
        }
        public static function computeStringSize(_arg1:TextFormat, _arg2:String, _arg3:Boolean=true, _arg4:TextField=null):IntDimension{
            if (_arg4){
                TEXT_FIELD_EXT.embedFonts = _arg4.embedFonts;
                TEXT_FIELD_EXT.antiAliasType = _arg4.antiAliasType;
                TEXT_FIELD_EXT.gridFitType = _arg4.gridFitType;
                TEXT_FIELD_EXT.sharpness = _arg4.sharpness;
                TEXT_FIELD_EXT.thickness = _arg4.thickness;
            };
            TEXT_FIELD_EXT.text = _arg2;
            TEXT_FIELD_EXT.setTextFormat(_arg1);
            if (_arg3){
                return (new IntDimension(Math.ceil(TEXT_FIELD_EXT.width), Math.ceil(TEXT_FIELD_EXT.height)));
            };
            return (new IntDimension(Math.ceil(TEXT_FIELD_EXT.textWidth), Math.ceil(TEXT_FIELD_EXT.textHeight)));
        }
        public static function computeStringSizeWithFont(_arg1:ASFont, _arg2:String, _arg3:Boolean=true):IntDimension{
            TEXT_FIELD_EXT.text = _arg2;
            _arg1.apply(TEXT_FIELD_EXT);
            if (_arg3){
                return (new IntDimension(Math.ceil(TEXT_FIELD_EXT.width), Math.ceil(TEXT_FIELD_EXT.height)));
            };
            return (new IntDimension(Math.ceil(TEXT_FIELD_EXT.textWidth), Math.ceil(TEXT_FIELD_EXT.textHeight)));
        }
        private static function layoutTextWidth(_arg1:String, _arg2:IntRectangle, _arg3:Number, _arg4:ASFont):String{
            var _local7:Number;
            var _local8:int;
            var _local9:int;
            var _local10:int;
            var _local11:int;
            var _local12:String;
            var _local13:int;
            if (_arg2.width <= _arg3){
                return (_arg1);
            };
            var _local5 = "...";
            var _local6:int = Math.round(inter_computeStringWidth(_arg4, _local5));
            if (_local6 > _arg3){
                _local6 = Math.round(inter_computeStringWidth(_arg4, ".."));
                if (_local6 > _arg3){
                    _arg1 = ".";
                    _arg2.width = Math.round(inter_computeStringWidth(_arg4, "."));
                    if (_arg2.width > _arg3){
                        _arg2.width = 0;
                        _arg1 = "";
                    };
                } else {
                    _arg1 = "..";
                    _arg2.width = _local6;
                };
                return (_arg1);
            };
            _local7 = _local6;
            _local8 = _arg1.length;
            _local9 = 0;
            _local10 = _local8;
            while (_local9 < _local10) {
                _local11 = (_local9 + ((_local10 - _local9) / 2));
                _local12 = _arg1.substring(0, _local11);
                _local13 = Math.ceil((_local7 + inter_computeStringWidth(_arg4, _local12)));
                if ((((_local9 == (_local11 - 1))) && ((_local9 > 0)))){
                    if (_local13 > _arg3){
                        _local12 = _arg1.substring(0, _local9);
                        _arg2.width = Math.ceil((_local7 + inter_computeStringWidth(_arg4, _arg1.substring(0, _local9))));
                    } else {
                        _arg2.width = _local13;
                    };
                    return ((_local12 + _local5));
                };
                if (_local11 <= 1){
                    if (_local13 <= _arg3){
                        _arg2.width = _local13;
                        return ((_local12 + _local5));
                    };
                    _arg2.width = _local7;
                    return (_local5);
                };
                if (_local13 < _arg3){
                    _local9 = _local11;
                } else {
                    if (_local13 > _arg3){
                        _local10 = _local11;
                    } else {
                        _arg1 = (_local12 + _local5);
                        _arg2.width = _local13;
                        return (_arg1);
                    };
                };
            };
            _arg2.width = _local7;
            return ("");
        }
        public static function layoutText(_arg1:ASFont, _arg2:String, _arg3:Number, _arg4:Number, _arg5:IntRectangle, _arg6:IntRectangle):String{
            var _local9:Number;
            var _local7:IntDimension = inter_computeStringSize(_arg1, _arg2);
            var _local8:Boolean = (((_arg2 == null)) || ((_arg2 == "")));
            if (_local8){
                _arg6.width = (_arg6.height = 0);
            } else {
                _arg6.width = Math.ceil(_local7.width);
                _arg6.height = Math.ceil(_local7.height);
            };
            if (!_local8){
                _local9 = _arg5.width;
                if (_arg6.width > _local9){
                    _arg2 = layoutTextWidth(_arg2, _arg6, _local9, _arg1);
                };
            };
            if (_arg4 == CENTER){
                _arg6.x = (_arg5.x + ((_arg5.width - _arg6.width) / 2));
            } else {
                if (_arg4 == RIGHT){
                    _arg6.x = (_arg5.x + (_arg5.width - _arg6.width));
                } else {
                    _arg6.x = _arg5.x;
                };
            };
            if (_arg3 == CENTER){
                _arg6.y = (_arg5.y + ((_arg5.height - _arg6.height) / 2));
            } else {
                if (_arg3 == BOTTOM){
                    _arg6.y = (_arg5.y + (_arg5.height - _arg6.height));
                } else {
                    _arg6.y = _arg5.y;
                };
            };
            return (_arg2);
        }
        public static function createPaneToHold(_arg1:Component, _arg2:LayoutManager, _arg3:Object=null):Container{
            var _local4:JPanel = new JPanel(_arg2);
            _local4.setOpaque(false);
            _local4.append(_arg1, _arg3);
            return (_local4);
        }
        public static function getPopupAncestor(_arg1:Component):JPopup{
            while (_arg1 != null) {
                if ((_arg1 is JPopup)){
                    return (JPopup(_arg1));
                };
                _arg1 = _arg1.getParent();
            };
            return (null);
        }
        public static function getOwnerAncestor(_arg1:Component):DisplayObjectContainer{
            if (_arg1 == null){
                return (null);
            };
            var _local2:JPopup = getPopupAncestor(_arg1);
            if (_local2 == null){
                return ((_arg1.root as DisplayObjectContainer));
            };
            return (_local2);
        }
        public static function getOwnerComponent(_arg1:DisplayObject):Component{
            while (((!((_arg1 == null))) && (!((_arg1 is Component))))) {
                _arg1 = _arg1.parent;
            };
            return ((_arg1 as Component));
        }
        static function weakRegisterComponent(_arg1:Component):void{
            weakComponentDic[_arg1] = null;
        }
        public static function updateAllComponentUIInMemory():void{
            var _local1:*;
            for (_local1 in weakComponentDic) {
                if (!_local1.isUIElement()){
                    _local1.updateUI();
                };
            };
        }
        public static function updateAllComponentUI(_arg1:Stage=null):void{
            if (_arg1 == null){
                _arg1 = AsWingManager.getStage();
            };
            if (AsWingManager.isStageInited()){
                updateChildrenUI(_arg1);
            };
        }
        public static function updateComponentTreeUI(_arg1:Component):void{
            updateChildrenUI(getOwnerAncestor(_arg1));
        }
        public static function updateChildrenUI(_arg1:DisplayObject):void{
            var _local3:DisplayObjectContainer;
            var _local4:int;
            if (_arg1 == null){
                return;
            };
            var _local2:Component = (_arg1 as Component);
            if (_local2){
                if (_local2.isUIElement()){
                    return;
                };
                _local2.updateUI();
            };
            if ((_arg1 is DisplayObjectContainer)){
                _local3 = DisplayObjectContainer(_arg1);
                _local4 = (_local3.numChildren - 1);
                while (_local4 >= 0) {
                    updateChildrenUI(_local3.getChildAt(_local4));
                    _local4--;
                };
            };
        }

        TEXT_FIELD.autoSize = TextFieldAutoSize.LEFT;
        TEXT_FIELD.type = TextFieldType.DYNAMIC;
        TEXT_FIELD_EXT.autoSize = TextFieldAutoSize.LEFT;
        TEXT_FIELD_EXT.type = TextFieldType.DYNAMIC;
    }
}//package org.aswing 
